$(function() {

    var that;

    var log;
    //log = console.log;

    function hideContent(){
        $('#text_cont').hide();
    }

    tinyMCE.init({

        mode : "textareas",
        theme : "advanced",
        plugins : "lists,inlinepopups,paste,directionality,xhtmlxtras",
        theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,bullist,outdent,indent,|,undo,redo,|,link,unlink,anchor,image,cleanup",
        theme_advanced_buttons2 : "",
        theme_advanced_buttons3 : "",
        theme_advanced_toolbar_location : "top",
        theme_advanced_toolbar_align : "left",
        theme_advanced_statusbar_location : "bottom",
        theme_advanced_resizing : false,
        content_css : base_url+"assets/js/tinymce/themes/advanced/skins/default/custom_content.css",
        oninit : hideContent

    });

    $('#page_id').change(function(){

        if($(this).val()==0) return false;

        that = $(this).busy({ position : 'right', hide : false });
        $('#text_cont').hide();

        $.ajax({
            type: 'POST',
            url: base_url+'cms/view_text',
            data:{ 
                id: $(this).val(),
                page: $('#text_cont input[name=pagetype]').val()
            },
            dataType: 'json',
            success: function(data){ 

                if (typeof data.row[0]==='undefined'){                                       

                    /* NO DATA */
                    that.busy('hide');
                    logm('Nema podataka');
                    $('#text_cont input[name=title]').val('');
                    tinyMCE.get('txt_strane').setContent('');
                    $('#text_cont').fadeIn();

                }else{

                    //log(data.row[0])
                    $('#text_cont').fadeIn();
                    $('#del').show();
                    that.busy('hide'); 

                    $('#text_cont input[name=actiontype]').val('edit');
                    $('#text_cont input[name=txtid]').val(data.row[0].id);
                    $('#text_cont input[name=title]').val(data.row[0].title);
                    tinyMCE.get('txt_strane').setContent(data.row[0].text);
                    $('#form_submit').text('Uredi');


                }



            },
            error:function(data){alert("Error: " + data);}
        });

        return false;

    })      

    $('#form_submit').live('click', function(){

        that = $(this).busy({ position : 'left', hide : false });

        var id = $('#text_cont input[name=txtid]').val();
        var page = $('#text_cont input[name=pagetype]').val(); 
        var action = $('#text_cont input[name=actiontype]').val(); 
        var title = $('#text_cont input[name=title]').val();
        var txt = tinyMCE.get('txt_strane').getContent();

        $.ajax({
            type: 'POST',
            url: base_url+'cms/edit_text',
            data:{ 
                id: id,
                page: page,
                action: action,
                title: title,
                text: txt
            },
            dataType: 'json',
            success: function(data){

                if(data.action==true){
                    $('#text_cont').hide();
                    $('#page_id').val(0); 

                    if(action=='new'){
                        $('<option value="'+data.lastid+'">'+title+'</option>').appendTo('#page_id');
                    }else if (action=='edit'){
                        $("#page_id option[value=" + id + "]").text(title);  
                    }

                    logm('Uspješna akcija');

                }else{
                    logm(data.msg);
                }


            },
            error:function(data){alert("Error: " + data);}
        });

        return false;

    });

    $('#form_delete').live('click', function(){

        that = $(this).busy({ position : 'left', hide : false });

        var id = $('#text_cont input[name=txtid]').val();

        $.ajax({
            type: 'POST',
            url: base_url+'cms/delete_text',
            data:{ 
                id: id
            },
            dataType: 'json',
            success: function(data){

                $('#text_cont').hide();
                $('#page_id').val(0); 

                $("#page_id option[value=" + id + "]").remove();  

                logm('Uspješna akcija');


            },
            error:function(data){alert("Error: " + data);}
        });

        return false;

    });


    function logm($msg){

        $('.logm').remove();
        that.busy('hide'); 

        var html = '<div class="box clearfix logm" style="display:none;border-color:#FF9900"><h4>Poruka sistema</h4><p></p></div>';

        $('.box:first').after(html);
        $('.box:nth-child(2) p').html($msg);
        $('.box:nth-child(2)').fadeIn('slow');

        $('html, body').animate({
            scrollTop: $(".box:nth-child(2)").offset().top
        }, 500);


        setTimeout("$('.box:nth-child(2)').fadeOut(function(){$(this).remove();})",2300);
    }


    $('#addnew').live('click', function(){

        $('#del').hide();

        that = $(this).busy({ position : 'center', hide : true });


        //logm(language['new'+sufix]);
        $('#text_cont input[name=title]').val('');
        tinyMCE.get('txt_strane').setContent('');
        $('#text_cont input[name=actiontype]').val('new');
        $('#form_submit').text('Dodaj');

        $('#text_cont').fadeIn(243,function(){$('#page_id').val(0);});
        
        that.busy('hide'); 


    });

    $('#page_id').val(0);

});